package com.jhd.fuzhou.view.special;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.jhd.fuzhou.service.SupervisionService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.view.daily.DocumentListActivity;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.SupervisedUnitVo;
import com.jhd.fuzhou.vo.SupervisionInspectionVo;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SpecialCompanyDetailActivity extends BaseActivity {
    private SupervisionInspectionVo supervisionInspectionVo;
    @BindView(R.id.tv_unitName)
    TextView tv_unitName;//被监督单位
    @BindView(R.id.tv_creditCode)TextView tv_creditCode;//社会信用代码
    @BindView(R.id.tv_status)TextView tv_status;//经验状态
    @BindView(R.id.tv_superviseOffice)TextView tv_superviseOffice;//监督机构
    @BindView(R.id.tv_superviseSector)TextView tv_superviseSector;//监督部门
    @BindView(R.id.tv_legalRepresentative)TextView tv_legalRepresentative;//法人代表
    @BindView(R.id.tv_gender)TextView tv_gender;//性别
    @BindView(R.id.tv_nation)TextView tv_nation;//民族
    @BindView(R.id.tv_contactNumber)TextView tv_contactNumber;//单位电话
    @BindView(R.id.tv_registerAddress)TextView tv_registerAddress;//注册地址
    @BindView(R.id.btn_add_report)
    Button btn_add_report;//添加报告
    @BindView(R.id.btn_editing_unit)Button btn_editing_unit;//编辑单位
    @BindView(R.id.btn_supervision_record)Button btn_supervision_record;//监督记录
    private String data;
    private Context context= this;
    private Gson gson=new Gson();
    private SupervisedUnitVo supervisedUnitVo;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_company_detail);
    }

    @Override
    protected void processLogic() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        data=getIntent().getStringExtra("data");
        if(StringUtil.isNullOrEmpty(data)==false){
            supervisionInspectionVo = JSON.parseObject(data, SupervisionInspectionVo.class);
            getSupervisedUnitById(supervisionInspectionVo.getSupervisedUnitId());
        }
    }

    private void setData(SupervisedUnitVo supervisedUnitVo){
        tv_unitName.setText(supervisedUnitVo.getUnitName());
        tv_creditCode.setText(supervisedUnitVo.getCreditCode());
        if(supervisedUnitVo.getBusinessStatus().equals("0")){
            tv_status.setText("关闭");
        }else if(supervisedUnitVo.getBusinessStatus().equals("1")){
            tv_status.setText("正常");
        }else if(supervisedUnitVo.getBusinessStatus().equals("2")){
            tv_status.setText("暂停");
        }else{
            tv_status.setText("未知");
        }
        tv_superviseOffice.setText(supervisedUnitVo.getSuperviseOffice());
        tv_superviseSector.setText(supervisedUnitVo.getSuperviseSector());
        tv_legalRepresentative.setText(supervisedUnitVo.getLegalRepresentative());
        tv_gender.setText(supervisedUnitVo.getGender());
        tv_nation.setText(supervisedUnitVo.getNation());
        tv_contactNumber.setText(supervisedUnitVo.getContactNumber());
        tv_registerAddress.setText(supervisedUnitVo.getRegisterAddress());
    }
    @OnClick({R.id.btn_add_report,R.id.btn_editing_unit,R.id.btn_supervision_record})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_add_report:
                Intent intent=new Intent(context, SpecialStepOneActivity.class);
                intent.putExtra("id", supervisionInspectionVo.getId());
                startActivity(intent);
                finish();
                break;
            case R.id.btn_editing_unit:

                break;
            case R.id.btn_supervision_record:
                Intent intent3=new Intent(context, DocumentListActivity.class);
                intent3.putExtra("type","3");
                intent3.putExtra("unitName",supervisionInspectionVo.getUnitName());
                intent3.putExtra("supervisionUnit",gson.toJson(supervisedUnitVo));
                startActivity(intent3);
                break;
        }
    }
    private void getSupervisedUnitById(String id){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("id",id);
        Call<JsonForResult> jsonForResultCall = vs.getSupervisedUnitById(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        supervisedUnitVo= JSON.parseObject(jsonForResult.getData().toString(), SupervisedUnitVo.class);
                        setData(supervisedUnitVo);
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}